package com.ushine.web.component.mybatis.encryption;

/**
 * @Author: yls
 * @Date 2022/2/10 17:07
 * @Description: 对配置文件进行加密解密
 * @Version 1.0
 **/

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * @Author: yls
 * @Date 2021/11/11 14:20
 * @Description: 加解密工具
 * @Version 1.0
 **/
@Slf4j
public class JasyptUtil {
    /**
     * 功能描述:
     * 〈 加密、解密 〉
     *
     * @param password 配置文件中设定的加密盐值
     * @param value    解密密文
     * @return : java.lang.String
     * @author : yls
     * @date : 2021/11/11 14:26
     */
    public static String encyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        return encryptor.encrypt(value);
    }


    /**
     * 解密
     *
     * @param password 配置文件中设定的加密盐值
     * @param value    解密密文
     * @return
     */
    public static String decyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        return encryptor.decrypt(value);
    }


    public static SimpleStringPBEConfig cryptor(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }

    public static void main(String[] args) {
        String slat = "yls-fight";
        // 加密
        String encPwd = encyptPwd(slat, "root");
        // 解密
        String decPwd = decyptPwd(slat, encPwd);
        log.info(encPwd);
        log.info(decPwd);

        // 加密
        String encUser = encyptPwd(slat, "root");
        // 解密
        String decUser = decyptPwd(slat, encUser);
        log.info(encUser);
        log.info(decUser);

        String primaryUrl = "jdbc:mysql://127.0.0.1:3306/fight_web_primary?userUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        String encPrimaryUrl = encyptPwd(slat,primaryUrl);
        log.info(encPrimaryUrl);

        String secondUrl = "jdbc:mysql://127.0.0.1:3306/fight_web_log?userUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
        String encSecondUrl = encyptPwd(slat, secondUrl);
        log.info(encSecondUrl);


//        String primaryPs6Url = "jdbc:p6spy:mysql://127.0.0.1:3306/fight_web_primary?userUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
//        String encPrimaryPs6Url = encyptPwd(slat,primaryPs6Url);
//        log.info(encPrimaryPs6Url);
//
//        String secondPs6Url = "jdbc:p6spy:mysql://127.0.0.1:3306/fight_web_log?userUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
//        String encSecondPs6Url = encyptPwd(slat,secondPs6Url);
//        log.info(encSecondPs6Url);
    }
}